|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
Listing 19.14 Objects and properties in MUSIC.FRM.
Begin VB.Form Form1
Caption = Music Database
Begin VB.CommandButton Command1
Caption = E&xit
Index = 4
End
Begin VB.CommandButton Command1
Caption = &List
Index = 3
End
Begin VB.CommandButton Command1
Caption = &Find a Record
Index = 2
End
Begin VB.CommandButton Command1
Caption = &Delete Current Record
Index = 1
End
Begin VB.CommandButton Command1
Caption = &Add a Record
Index = 0
End
Begin VB.TextBox txtNotes
DataField = Notes
DataSource = Data1
End
Begin VB.TextBox txtLocation
DataField = Location
DataSource = Data1
End
Begin VB.TextBox txtPublisher
DataField = Publisher
DataSource = Data1
End
Begin VB.TextBox txtMedia
DataField = Media
DataSource = Data1
End
Begin VB.TextBox txtTitle
DataField = Title
DataSource = Data1
End
Begin VB.TextBox txtComposer
DataField = Composer/Group
DataSource = Data1
End
Begin VB.Data Data1
Connect = Access
Exclusive = 0 False
Options = 0
RecordsetType = 1 Dynaset
End
Begin VB.Label Label6
Alignment = 1 Right Justify
Caption = Notes:
End
Begin VB.Label Label5
Alignment = 1 Right Justify
Caption = Location:
End
Begin VB.Label Label4
Alignment = 1 Right Justify
Caption = Publisher:
End
Begin VB.Label Label3
Alignment = 1 Right Justify
Caption = Media:
End
Begin VB.Label Label2
Alignment = 1 Right Justify
Caption = Title:
End
Begin VB.Label Label1
Alignment = 1 Right Justify
Caption = Composer/group:
End
End
Binding Controls To The Data TableOnce the form has been designed, you need to bind the controls to the data. This requires two steps:
As you might expect, you do this with the control properties. For the Data control, set its DatabaseName property to the database you just created, MUSIC.MDB, and set its RecordSource property to Recordings. (Because a database can have more than one table, you must specify the exact table to which the Data control is linked.) Also, set the Data controls RecordsetType property to 1 -Dynaset (more on Recordsets and Dynasets soon). For each of the six Text Box controls, set the DataSource property to Data1, pointing at the Data control. Then, set the DataField property of each control to the name of its associated field: Composer/Group for the first Text Box, Title for the second, and so on. Although the program is not complete, we can run it and see the power of data-aware controls in action. The dummy record we entered in the database from the Visual Data Manager will be displayed in the Text Box controls. Had we entered multiple records, we would be able to move among them by clicking on the arrows on the Data controlall this without a single line of code. We do, however, need some codealthough surprisingly littleto complete the database programs functionality. Adding The Remaining CodeThe code for the array of Command Buttons is remarkably simple, largely because of the power of the database engine. This code is shown in Listing 19.15. To add a new record, you just call the AddNew method of the Recordset object associated with the Data control. Other than the convenient touch of setting the focus to the first Text Box, thats all there is to it. The AddNew method takes care of clearing the Text Boxes in preparation for entry of new data, as well as saving the new data once it has been entered. Compare this with the code for adding a new record in this chapters first programwhat a difference. Deleting a record is equally easy. Executing the Recordset objects Delete method is all that is required. Follow this with the MoveNext method, which displays the next record, so the forms Text Boxes do not remain blank. Finding a particular record requires a call to the FindRecord procedure, which well create soon. Likewise, for listing all records, you rely on the Show method to display a separate form. Listing 19.15 The Command Button Click event code.
Private Sub Command1_Click(Index As Integer)
Select Case Index
Case 0 Add
txtComposer.SetFocus
Data1.Recordset.AddNew
Case 1 Delete
Data1.Recordset.Delete
Data1.Recordset.MoveNext
Case 2 Find
Call FindRecord
Case 3 List
lstForm.Show
Case 4 Exit
End
End Select
End Sub
Recordsets A Recordset is a type of Visual Basic object that acts as an abstract representation of an actual database table on disk. Your program accesses and manipulates the data in the table by means of the Recordset object, which has its own properties and methods. When a form containing a Data control is loaded, and if that Data control is validly linked to a table in a database, a Recordset object is automatically created and available for use by means of the Data controls Recordset property. A Recordset can also be created in codeindependent of a Data controlbut we will not explore this technique in this chapter. For a complete look at the many properties and methods of the Recordset object, refer to the Visual Basic Help system.
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |